/**
 * Created by Trialiet on 2016/8/22.
 */

$(document).ready(function () {
    $.getJSON("initMenu.action", function (data) {
        console.log(data);
        $.each(data.result, function (index, object) {
            showMenu(object);
        });
    });
})

function removeDish(id, price, cate) {
    if (cate == "堂食"){
        var dish = "#1cart" + id;
    }
    else{
        var dish = "#0cart" + id;
    }
    if ( $(dish).length > 0) {
        $(dish).find("#count").text(parseInt($(dish).find("#count").text()) - 1);
        if(parseInt($(dish).find("#count").text()) <= 0){
            $(dish).remove();
        }
        $(dish).find("#amount").text($(dish).find("#count").text() * price);
    }
    $("#need").text(parseInt($("#need").text()) - price);
}

function addDish(id, name, price, cate) {
    if (cate == "堂食")
        var dish = "1cart" + id;
    else
        var dish = "0cart" + id;
    if ( $("#" + dish).length > 0) {
        $("#" + dish).find("#count").text(parseInt($("#" + dish).find("#count").text()) + 1);
        $("#" + dish).find("#amount").text($("#" + dish).find("#count").text() * price);
    }
    else {
        $("#cart").append("<tr id='" + dish + "' name='" + id + "'>" +
            "<td align='center' id='name'>" +
            name + "</td>" +
            "<td align='center' name='count'> " +
            "<span class='glyphicon glyphicon-plus' onclick=addDish(" +
            id +
            ",'" +
            name +
            "'," +
            price + ",'" + cate + "'" +
            ")></span>" +
            "<span id='count' name='count'>1</span>" + 
            "<span class='glyphicon glyphicon-minus' onclick=removeDish(" + id + 
            "," + price + 
            ",'" + cate + 
            "')></span>" + 
            "</td>" +
            "<td align='center' name='cate'>" + cate +"</td>" +
            "<td align='center' id='amount'> " + price + "</td>" +
            "</tr>");
    }
    $("#need").text(parseInt($("#need").text()) + price);
}

function zhaoling() {
    $("#zhaoling").text(parseInt($("#receive").val()) - parseInt($("#need").text()));
}

function showMenu(o) {
    $("#menu").append("<tr id='menu" + o.id + "'>" +
        "<td align='center'>" + o.name + "</td>" +
        "<td align='center'>" + o.price + "</td>" +
        "<td align='center'>" +
        "<button type='button' class='btn btn-default' onclick=addDish(" + 
        o.id + 
        ",'" + 
        o.name + 
        "'," + 
        o.price +
        ",'堂食'" +
        ")>" +
        "<span class='glyphicon glyphicon-ok'></span></button>" +
        "</td>" +
        "<td align='center'>" +
        "<button type='button' class='btn btn-default' onclick=addDish(" +
        o.id +
        ",'" +
        o.name +
        "'," +
        o.price +
        ",'外卖'" +
        ")>" +
        "<span class='glyphicon glyphicon-ok'></span></button>" +
        "</td>" +
        "</tr>");
}

function complete() {
    var receive = parseInt($("#receive").val());
    var need = parseInt($("#need").text());
    if (receive < need || need <= 0 ){
        alert("收款金额非法！");
        return;
    }
    var jsonArray = [];
    $("#cart").find("tr").each(function () {        //每一行
        //获取id，id存在name属性中
        var id, amount, cate;
        id = parseInt($(this).attr("name"));
        $(this).find("td").each(function () {       //每一单元格
            //获取count
            if ($(this).attr("name") == "count") {
                amount = parseInt($(this).text());
            }
            //获取cate
            if ($(this).attr("name") == "cate") {
                cate = $(this).text();
            }
        });
        var json = $.parseJSON('{"dishId": ' + id + ', "amount": ' + amount + ', "cate": \"' + cate + '\"}');
        jsonArray.push(json);
    });
    var data = JSON.stringify(jsonArray);
    var flag = confirm("应收：" + need + "\n实收：" + receive + "\n确认？");
    if (flag == true) {
        $.ajax({
            type: "post",
            url: "complete.action",
            data: "{\"data\":" + data + ", \"receive\":" + receive + "}",
            contentType: "application/json;charset=utf-8",
            dataType: 'json',
            async: false,
            complete: function () {
                alert("下单成功！");
                cancel();
            }
        });
    }
}

function cancel() {
    $("#cart").html("");
    $("#need").text(0);
    $("#receive").val(0);
}